package com.main.rentsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.main.rentsystem.mapper.UserMapper;
import com.main.rentsystem.pojo.dto.UserPageQueryDTO;
import com.main.rentsystem.pojo.entity.Message;
import com.main.rentsystem.pojo.entity.PageBean;
import com.main.rentsystem.pojo.entity.User;
import com.main.rentsystem.pojo.vo.MessageVO;
import com.main.rentsystem.service.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.main.rentsystem.controller.user.UserController.USER_ID;

@Service
public class UserServerimpl implements UserServer {


    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param userx
     * @return
     */
    public User login(User userx) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("username", userx.getUsername());
        map.put("password", userx.getPassword());
        List<User> users = userMapper.selectByMap(map);
        if(users != null && users.size() > 0) {
            return users.get(0);
        }
        return null;
    }

    /**
     * 判断账号是否存在
     * @param user
     * @return
     */
    public User isExist(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 用户注册
     * @param user
     */
    public void register(User user) {
        userMapper.insert(user);
    }

    /**
     * 用户留言
     * @param message
     */
    public void PutMessage(Message message) {
        message.setUserId(USER_ID);
        message.setPostTime(LocalDate.now());
        userMapper.InsertMseeage(message);
    }

    /**
     * 我的留言
     * @return
     */
    public List<MessageVO> GetMessage(User user) {

        List<Message> messageList = userMapper.GetMessage(user);
        List<MessageVO> messageVOList = new ArrayList<>();
        for(Message message : messageList){
            MessageVO messageVO = new MessageVO();
            messageVO.setContent(message.getContent());
            messageVO.setPostTime(message.getPostTime());
            User users = userMapper.getUserById(message.getUserId());
            messageVO.setUserName(users.getName());
            messageVOList.add(messageVO);
        }
        return messageVOList;
    }

    /**
     * 获取所有用户信息
     * @return
     */
    public PageBean getUserInfo(UserPageQueryDTO userPageQueryDTO) {

        PageHelper.startPage(userPageQueryDTO.getPage(),userPageQueryDTO.getPageSize());
        Page<User> page = userMapper.getUserInfo(userPageQueryDTO);
        return new PageBean(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除用户
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        userMapper.deleteBatch(ids);
    }

    /**
     * 根据id修改用户信息
     * @param user
     */
    public void updateById(User user) {
        userMapper.updateById(user);
    }

    /**
     * 修改密码
     * @param user
     * @param newPassword
     */
    public void changePassword(User user, String newPassword) {
        userMapper.changPassword(user,newPassword);
    }

    /**
     * 查询用户
     * @param id
     * @return
     */
    public User getUserById(Long id) {
        return userMapper.getUserById(id);
    }

}
